﻿using System;
using System.Windows.Input;

namespace XCodeGenerator
{
    /// <summary>
    /// The CommandHandler class - an ICommand that can fire a function.
    /// </summary>
    public class CommandHandler : ICommand
    {
        private Action<string> _action;
        private bool _canExecute;

        /// <summary>
        /// Initializes a new instance of the <see cref="Command"/> class.
        /// </summary>
        /// <param name="parameterizedAction">The parameterized action.</param>
        /// <param name="canExecute">if set to <c>true</c> [can execute].</param>
        public CommandHandler(Action<string> action, bool canExecute = true)
        {
            //  Set the action.
            this._action = action;
            this._canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _action(parameter as string);
        }
    }
}
